package com.xingfly.web;

import com.xingfly.model.Event;
import com.xingfly.model.Like;
import com.xingfly.model.Notification;
import com.xingfly.model.User;
import com.xingfly.security.jwt.annotation.CurrentUser;
import com.xingfly.service.AccountService;
import com.xingfly.service.EventService;
import com.xingfly.service.LikeService;
import com.xingfly.service.NotificationService;
import com.xingfly.util.Type;
import com.xingfly.web.model.ResponseModel;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by SuperS on 04/01/2017.
 */
@RestController
@RequestMapping("/likes")
public class LikeController {
    @Autowired
    private LikeService likeService;
    @Autowired
    private EventService eventService;
    @Autowired
    private NotificationService notificationService;
    @Autowired
    private AccountService accountService;

    @PostMapping
    @Transactional
    public ResponseModel<?> like(HttpServletRequest request, @CurrentUser User me) {
        String authorIdStr = request.getHeader("authorId");
        String objectTypeStr = request.getHeader("objectType");
        String objectIdStr = request.getHeader("objectId");

        Long authorId = null;
        Integer objectType = null;
        Long objectId = null;

        Like like = null;

        if (!(authorIdStr != null
                && objectIdStr != null && objectTypeStr != null)) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "参数格式不正确!");
        }
        try {
            authorId = Long.parseLong(authorIdStr);
            objectType = Integer.parseInt(objectTypeStr);
            objectId = Long.parseLong(objectIdStr);
        } catch (NumberFormatException e) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "参数格式不正确!");
        }
        Event event = eventService.getEvent(objectType, objectId);
        if (event == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "对象不存在!");
        }
        User author = accountService.find(authorId);
        if (author == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "作者不存在!");
        }
        if (!likeService.isLike(me.getId(), objectType, objectId)) {
            like = likeService.like(me.getId(), objectType, objectId);
        } else {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "已like该对象,like失败!");
        }
        Notification notification = new Notification(Type.NOTIFY_TYPE_LIKE, 0L, event, author, me, 0);
        notificationService.doNotify(notification);
        return ResponseModel.create(like, HttpStatus.OK, "like成功!");
    }

    @DeleteMapping
    public ResponseModel<?> unLike(HttpServletRequest request, @CurrentUser User me) {
        String authorIdStr = request.getHeader("authorId");
        String objectTypeStr = request.getHeader("objectType");
        String objectIdStr = request.getHeader("objectId");
        Long authorId = null;
        Integer objectType = null;
        Long objectId = null;
        try {
            authorId = Long.parseLong(authorIdStr);
            objectType = Integer.parseInt(objectTypeStr);
            objectId = Long.parseLong(objectIdStr);
        } catch (NumberFormatException e) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "参数格式不正确!");
        }
        if (!(authorIdStr != null
                && objectIdStr != null && objectTypeStr != null)) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "参数格式不正确!");
        }
        Event event = eventService.getEvent(objectType, objectId);
        if (event == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "对象不存在!");
        }
        User author = accountService.find(authorId);
        if (author == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "作者不存在!");
        }
        if (likeService.isLike(me.getId(), objectType, objectId)) {
            likeService.undoLike(me.getId(), objectType, objectId);
        } else {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "未like该对象,取消失败!");
        }
        return ResponseModel.create(null, "取消喜欢成功!");
    }
}
